﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.Reporting.WebForms;
using VSM.Business.Printing.Reports;
using VSM.Utils;

namespace VSM
{
    public partial class ListReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                hdnReportName.Value = Request.QueryString["rptName"];
                try
                {
                    switch (hdnReportName.Value)
                    {
                        case "CriteriaSummaryOfInventoryCard.ascx":
                            CriteriaSummaryOfInventoryCard ucCriteriaSummaryOfInentory = (CriteriaSummaryOfInventoryCard)Page.LoadControl(hdnReportName.Value);
                            ucCriteriaSummaryOfInentory.btnViewReportClk -= new btnViewReport_Click(btnViewReportClk);
                            ucCriteriaSummaryOfInentory.btnViewReportClk += new btnViewReport_Click(btnViewReportClk);
                            this.pnlListReport.Controls.Add(ucCriteriaSummaryOfInentory);
                            Session["CriteriaSummaryOfInventoryCard.ascx"] = ucCriteriaSummaryOfInentory;
                            break;

                        case "CriteriaVehiclesInOutStockAtVSMDealers.ascx":
                            CriteriaVehiclesInOutStockAtVSMDealers ucCriteriaVehiclesInOutStockAtVSMDealers = (CriteriaVehiclesInOutStockAtVSMDealers)Page.LoadControl(hdnReportName.Value);
                            ucCriteriaVehiclesInOutStockAtVSMDealers.btnViewReportClk -= new btnViewReport_Click(btnViewReportClk);
                            ucCriteriaVehiclesInOutStockAtVSMDealers.btnViewReportClk += new btnViewReport_Click(btnViewReportClk);
                            this.pnlListReport.Controls.Add(ucCriteriaVehiclesInOutStockAtVSMDealers);
                            Session["CriteriaVehiclesInOutStockAtVSMDealers.ascx"] = ucCriteriaVehiclesInOutStockAtVSMDealers;
                            break;
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        void btnViewReportClk(object sender, EventArgs e)
        {
            try
            {
                string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                DataTable dtReport = null, dtSubReport = null, dtSubReport1 = null, dtSubReport2 = null, dtSubReport3 = null;
                string rptTemplatePath = string.Empty, lsReportName, lsSubReportName, lsSubReportName1, lsSubReportName2, lsSubReportName3;
                lsReportName = lsSubReportName = lsSubReportName1 = lsSubReportName2 = lsSubReportName3 = string.Empty;

                DataSet dsParamArrays = new DataSet();
                DataTable dtParamArrays = new DataTable();
                DataRow dr = null;

                switch (hdnReportName.Value)
                {
                    case "CriteriaSummaryOfInventoryCard.ascx":
                        rptTemplatePath = "Sales_rpt_SummaryOfInventoryCard.rpt";
                        CriteriaSummaryOfInventoryCard ucCriteriaSummaryOfInentory = (CriteriaSummaryOfInventoryCard)Session["CriteriaSummaryOfInventoryCard.ascx"];
                        dtParamArrays.Columns.Add("key");
                        dtParamArrays.Columns.Add("value");
                        dr = dtParamArrays.NewRow();
                        // @FromDate parameter
                        dr["key"] = "@FromDate"; dr["value"] = Utils.AppConstants.ConvertToDate(ucCriteriaSummaryOfInentory.FromDate.Text); 
                        dtParamArrays.Rows.Add(dr);
                        // @ToDate parameter
                        dr = dtParamArrays.NewRow();
                        dr["key"] = "@ToDate"; dr["value"] = Utils.AppConstants.ConvertToDate(ucCriteriaSummaryOfInentory.ToDate.Text); // 
                        dtParamArrays.Rows.Add(dr);
                        // @WhsCode parameter
                        dr = dtParamArrays.NewRow();
                        dr["key"] = "@WhsCode"; dr["value"] = ucCriteriaSummaryOfInentory.Warehouse.SelectedValue;
                        dtParamArrays.Rows.Add(dr);
                        // @GroupCode parameter
                        dr = dtParamArrays.NewRow();
                        dr["key"] = "@GroupCode"; dr["value"] = "101,103";
                        dtParamArrays.Rows.Add(dr);

                        dsParamArrays.Tables.Add(dtParamArrays.Copy());
                        if (ucCriteriaSummaryOfInentory.Warehouse.SelectedValue.Equals("HCM-SAL")
                            || ucCriteriaSummaryOfInentory.Warehouse.SelectedValue.Equals("HN-SAL") || ucCriteriaSummaryOfInentory.Warehouse.SelectedValue.Equals("KTB"))
                            dtReport = GF.GetSummaryOfInventory("sp_rpt_AI_InventoryBalance", dsParamArrays, Utils.AppConstants.VSM);
                        else
                            dtReport = GF.GetSummaryOfInventory("sp_rpt_AI_InventoryBalance", dsParamArrays, Utils.AppConstants.DEALER);

                        dtReport.TableName = "sp_rpt_AI_InventoryBalance"; 
                        break;

                    case "CriteriaVehiclesInOutStockAtVSMDealers.ascx":
                        rptTemplatePath = "Sales_VehiclesInOutStockAtVSMDealer.rpt";

                        CriteriaVehiclesInOutStockAtVSMDealers ucCriteriaVehiclesInOutStockAtVSMDealers = (CriteriaVehiclesInOutStockAtVSMDealers)Session["CriteriaVehiclesInOutStockAtVSMDealers.ascx"];
                        dtParamArrays.Columns.Add("key");
                        dtParamArrays.Columns.Add("value");
                        dr = dtParamArrays.NewRow();
                        // @FromDate parameter
                        dr["key"] = "@FromDate"; dr["value"] = Utils.AppConstants.ConvertToDate(ucCriteriaVehiclesInOutStockAtVSMDealers.FromDate.Text); 
                        dtParamArrays.Rows.Add(dr);
                        // @ToDate parameter
                        dr = dtParamArrays.NewRow();
                        dr["key"] = "@ToDate"; dr["value"] = Utils.AppConstants.ConvertToDate(ucCriteriaVehiclesInOutStockAtVSMDealers.ToDate.Text); // 
                        dtParamArrays.Rows.Add(dr);
                        // @GroupCode parameter
                        dr = dtParamArrays.NewRow();
                        dr["key"] = "@GroupCode"; dr["value"] = "101,103";
                        dtParamArrays.Rows.Add(dr);

                        dsParamArrays.Tables.Add(dtParamArrays.Copy());
                        if (ucCriteriaVehiclesInOutStockAtVSMDealers.DBName.SelectedValue.Equals(Utils.AppConstants.VSM))
                            dtReport = GF.GetSummaryOfInventory("sp_rpt_AI_VehiclesInOutStockAtVSMDealers", dsParamArrays, Utils.AppConstants.VSM);
                        else
                            dtReport = GF.GetSummaryOfInventory("sp_rpt_AI_VehiclesInOutStockAtVSMDealers", dsParamArrays, Utils.AppConstants.DEALER);

                        dtReport.TableName = "sp_rpt_AI_VehiclesInOutStockAtVSMDealers";
                        break;
                }

                // Initial report template
                ReportDocument rptDoc = new ReportDocument();
                CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
                CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
                rptDoc.Load(Server.MapPath(rptTemplatePath));

                // Load data
                if (dtReport != null) rptDoc.SetDataSource(dtReport);

                if (dtSubReport != null) rptDoc.Subreports[lsSubReportName].SetDataSource(dtSubReport);
                if (dtSubReport1 != null) rptDoc.Subreports[lsSubReportName1].SetDataSource(dtSubReport1);
                if (dtSubReport2 != null) rptDoc.Subreports[lsSubReportName2].SetDataSource(dtSubReport2);
                if (dtSubReport3 != null) rptDoc.Subreports[lsSubReportName3].SetDataSource(dtSubReport3);
                // Binding
                CrystalReportViewer1.ReportSource = rptDoc;
                CrystalReportViewer1.RefreshReport();
                CrystalReportViewer1.DataBind();

            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
                lblError.Visible = true;
            }
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            btnViewReportClk(null, null);
        }

        //private void LoadFormPrinting(string dbType)
        //{
        //    GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
        //    DataTable dtReport = null, dtSubReport = null, dtSubReport1 = null, dtSubReport2 = null, dtSubReport3 = null;
        //    string rptTemplatePath = string.Empty, lsReportName, lsSubReportName, lsSubReportName1, lsSubReportName2, lsSubReportName3;
        //    lsReportName = lsSubReportName = lsSubReportName1 = lsSubReportName2 = lsSubReportName3 = string.Empty;

        //    try
        //    
        //        switch ("hdnDocType.Value")
        //        {
        //            case "18": // AP INVOICE
        //                break;

        //            case "19": // AP CREDIT
        //                break;

        //            case "20": // GOODS RECEIPT PO
        //                break;

        //            case "21": // GOODS RETURN
        //                break;

        //            case "22": // PURCHASE ORDER
        //                if(dbType.Equals(Utils.AppConstants.VSM))
        //                {
        //                    rptTemplatePath = "Sales_PO_VSM_MainReport.rpt";
        //                    //dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Dealer where DocEntry = " + hdnDocEntry.Value);
        //                    dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_TotalOrderQty where DocEntry = " + hdnDocEntry.Value);
        //                    dtSubReport1 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_ColorByColor where DocEntry = " + hdnDocEntry.Value);
        //                    dtSubReport2 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_ShippingSch where DocEntry = " + hdnDocEntry.Value);
        //                    dtSubReport3 = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_VSM_Forecasts where DocEntry = " + hdnDocEntry.Value);
        //                    lsSubReportName = "Sales_PO_VSM_TotalOrderQty.rpt";
        //                    lsSubReportName1 = "Sales_PO_VSM_ColorByColor.rpt";
        //                    lsSubReportName2 = "Sales_PO_VSM_Shipping Sch.rpt";
        //                    lsSubReportName3 = "Sales_PO_VSM_Forecast.rpt";
        //                }
        //                else
        //                {
        //                    rptTemplatePath = "Sales_PO_Dealer.rpt";
        //                    dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Dealer where DocEntry = " + hdnDocEntry.Value);
        //                    dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_PO_Detail_Dealer where DocEntry = " + hdnDocEntry.Value);
        //                    lsSubReportName = "Sales_PO_Detail_Dealer.rpt";
        //                }
        //                break;
        //            case "540000006": // PURCHASE QUOTATION"
        //                break;

        //            case "1470000113": // PURCHASE REQUEST"
        //                break;

        //            //-------------------SALES--------------------------
        //            case "13": // AR INVOICE
        //                break;

        //            case "14": // AR CREDIT
        //                break;

        //            case "15": // DELIVERY
        //                rptTemplatePath = "Sales_DeliveryOrder.rpt";
        //                dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_DeliveryOrder_VSM where DocEntry = " + hdnDocEntry.Value);
        //                break;

        //            case "16": // RETURN
        //                break;

        //            case "17": // SALES ORDER
        //                rptTemplatePath = "SalesConfirmedOrder.rpt";
        //                dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_OrderConfirmed where DocEntry = " + hdnDocEntry.Value);
        //                break;

        //            case "23": // SALES QUOTATION
        //                rptTemplatePath = "SalesQuotation.rpt";
        //                dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_SalesQuotation where DocEntry = " + hdnDocEntry.Value);
        //                dtSubReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_SalesFreight where DocEntry = " + hdnDocEntry.Value);
        //                lsSubReportName = "SalesQuotationFreight.rpt";
        //                break;

        //            //---------------------INVENTORY----------------------
        //            case "59": // GOODS RECEIPT
        //                break;

        //            case "60": // GOODS ISSUE
        //                break;

        //            case "67": // TRANSFER
        //                if (hdnPrintType.Value.Equals("1"))
        //                {
        //                    rptTemplatePath = "Sales_InternalTransfer_Invoice.rpt";
        //                    dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_InternalTransfer_Invoice where DocEntry = " + hdnDocEntry.Value);
        //                }
        //                else if (hdnPrintType.Value.Equals("2"))
        //                {
        //                    rptTemplatePath = "Sales_Consignment_StockoutSlip.rpt";
        //                    dtReport = GF.GetMasterListBySqlCommand(dbType, "select * from vw_Sal_Consignment_StockoutSlip where DocEntry = " + hdnDocEntry.Value);
        //                }
        //                break;

        //            case "1250000001": // TRANSFER REQUEST
        //                break;

        //            //-------------------MASTER DATA------------------
        //            case "171": // EMPLOYEE
        //                break;
        //            case "2": // BUSINESS PARTNER
        //                break;

        //            case "4": // ITEM
        //                break;

        //            case "11": // CONTACT PERSON
        //                break;

        //            case "12": // USER
        //                break;

        //            //----------------------OTHERS--------------------
        //            case "221": // Attachment
        //                break;

        //            case "97": // Sales opportunity
        //                break;

        //            case "33": // Activity
        //                break;

        //            case "30": // Journal Entry
        //                break;

        //            // -------------------PRODUCTION--------------------
        //            case "202": // Production Order
        //                break;

        //            case "198": // SALES FORECASTS
        //                break;

        //            //--------------------PAYMENT----------------------
        //            case "24": // Incoming
        //                break;

        //            case "46": // Outgoing
        //                break;
        //        }
        //        // Initial report template
        //        ReportDocument rptDoc = new ReportDocument();
        //        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        //        CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        //        rptDoc.Load(Server.MapPath(rptTemplatePath));

        //        // Load data
        //        if (dtReport != null) rptDoc.SetDataSource(dtReport);

        //        if (dtSubReport != null) rptDoc.Subreports[lsSubReportName].SetDataSource(dtSubReport);
        //        if (dtSubReport1 != null) rptDoc.Subreports[lsSubReportName1].SetDataSource(dtSubReport1);
        //        if (dtSubReport2 != null) rptDoc.Subreports[lsSubReportName2].SetDataSource(dtSubReport2);
        //        if (dtSubReport3 != null) rptDoc.Subreports[lsSubReportName3].SetDataSource(dtSubReport3);
        //        // Binding
        //        CrystalReportViewer1.ReportSource = rptDoc;
        //        CrystalReportViewer1.RefreshReport();
        //        CrystalReportViewer1.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        this.lblError.Text = ex.Message;
        //    }
        //}

    }
}